<?php

defined('BASEPATH') or die('forbidden');

class Realtime_Controller Extends Controller {

    public $statelist = array();
    private $wholeServerHost;
    private $server_info;
    private $m;
    function _init() {//var_dump($_SESSION['WG']);
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        $this->statelist = array(
            'new' => translate('AREA_STATUS_NEW'),
            'normal' => translate('AREA_STATUS_NORMAL'),
            'hide' => translate('AREA_STATUS_HIDE'),
            'close' => translate('AREA_STATUS_CLOSE'),
            'prepare' => translate('AREA_STATUS_PREPARE')
        );
        $config = get_config();
        $this->wholeServerHost = $config['host_url'];
        $selectSql = "SELECT * FROM server_mapping WHERE dbtype='6'";
        $this->db->query($selectSql);
        $this->server_info=$this->db->get_array('default');

        // db数据库
	echo "username {$this->server_info['username']} user passwd {$this->server_info['userpwd']} ip url {$this->server_info['dbip']}";
        //$this->m = new MongoClient("mongodb://{$this->server_info['username']}:{$this->server_info['userpwd']}@{$this->server_info['dbip']}");
        $this->m = new MongoClient("mongodb://MCGame:123456@{$this->server_info['dbip']}");
    }

    function index() {
        $this->view->display("area/realtime/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        //$areadata = array();
        $post_data='{"SessionID":"ewewqw","SessionKey":"wqqew"}';
        $result = vpost($this->wholeServerHost.'gm_server_list',3,$post_data);
        $results=json_decode($result,true);
        $total=count($results['SvrList']);
        $collection=$this->m->{$this->server_info['dbname']}->Account;
        $all_account=$collection->count();
        $today_login=$collection->count(array( 'lasttime' =>array('$gte'=>strtotime(date('Y-m-d')))));
        $seven_res=$collection->count(array( 'createtime' =>array('$gte'=>strtotime('-7 days'))));
        $data=array();
        foreach ($results['SvrList'] as $k=>$v){
            $v['SvrOpenTime'] = get_datetime($v['SvrOpenTime']);
            $re = vpost($v['SvrOutAddr'].'/gm_online_num',3,$post_data);
            $res=json_decode($re,true);
            $v['currentonline']=$res['Num'];
            $v['registercount']=$all_account;
            $v['todaycount']=$today_login;
            $v['sevenres']=$seven_res;
            $data[$k]=array('id' => $v['SvrID'], 'cell' => $v);
        }
//        $rows = array();
//        $this->db->query("SELECT * FROM {pre}area_current");
//        while ($row = $this->db->get_array()) {
//            $rows[$row['id']] = $row;
//        }
       // $total = count($results['SvrList']);
//        $all = array('currentonline' => 0, 'maxonline' => 0, 'registercount' =>0);
//        foreach ($data as $key => $value) {
//            if(time() - strtotime($value['updatetime']) > 30){
//                $rows[$key]['state'] =  translate('AREA_STATUS_CLOSE');
//            }else{
//                $rows[$key]['state'] =  translate('AREA_STATUS_NORMAL');
//                $all['currentonline']+=$value['currentonline'];
//            }
//
//            $all['maxonline']+=$value['maxonline'];
//            $all['registercount']+=$value['registercount'];
//        }
//        foreach ($rows as $key => $value) {
//            if(time() - strtotime($rows[$key]['updatetime']) > 30){
//                $rows[$key]['state'] = "<label style='color:#999999'>".translate('AREA_STATUS_CLOSE')."</label>" ;
//                $rows[$key]['maxonline'] = "<label style='color:#999999'>".$rows[$key]['maxonline']."</label>";
//                $rows[$key]['registercount'] = "<label style='color:#999999'>".$rows[$key]['registercount']."</label>";
//                $rows[$key]['currentonline'] = "<label style='color:#999999'>0</label>";
//                $rows[$key]['updatetime'] = "<label style='color:#999999'>".$rows[$key]['updatetime']."</label>";
//            }
//        }
//        $var['id'] = 'total';
//        $var['name'] = '全部';
//        $var['state'] = '';
//        $var['currentonline'] = $all['currentonline'];
//        $var['maxonline'] = $all['maxonline'];
//        $var['registercount'] = $all['registercount'];
//        array_unshift($rows, $var);
//
//        foreach ($rows as $key => $value) {
//
//            $row[] = array('id' => $value['id'], 'cell' => $value);
//        }

        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $data
        );
        echo json_encode($result);
    }

    function multi() {
        $type = $this->input->post("type");
        $ids = $this->input->post("ids");
        $c = 0;
        $r = '';
        $allids = $ids = explode(',', $ids);
        if (count($ids) == 0) {
            $r = translate('C_INVALID_PARAMS');
        } else {
            $ids = "'" . implode("','", $ids) . "'";
            if ($type == 'accept') {
                $this->db->query("UPDATE {pre}areasample SET state='normal' WHERE id IN ({$ids})");

                insert_log(translate('MENU_AREA_REALTIME'), translate('MULTI_OPEN'), array('ids'=>implode(",", $allids)));
                $c = 1;
            } else if ($type == 'reject') {
                $this->db->query("UPDATE {pre}areasample SET state='close' WHERE id IN ({$ids}) AND state!='hide'");
                insert_log(translate('MENU_AREA_REALTIME'),translate('MULTI_CLOSE'), array('ids'=> implode(",", $allids)));
                $c = 1;
            } else {
                $r = translate('C_INVALID_PARAMS');
            }
        }
        json_output($c, $r);
    }

}
